package olap4cloud.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GenDataOverStruct {

	private Random random = new Random();
	private long startTime;
	private long stopTime;
	private final int MIN_INT_GUID_DEFAULT = 1000000000;
	private final int MAX_INT_GUID_DEFAULT = 2147483647;

	private final long MIN_IP_DEFAULT = 1908367360l;
	private final long MAX_IP_DEFAULT = 3663989248l;
	
	private final int MAX_CHANNELID_LENGTH = 20;
	
	private final int MAX_COUNTRY_LENGTH = 200;

	private final String[] listDomain = { "edition.cnn.com", "nytimes.com",
			"bbc.co.uk", "news.yahoo.com", "news.google.com",
			"washingtonpost.com", "reuters.com", "drudgereport.com",
			"usatoday.com", "abcnews.go.com", "latimes.com" };
	
	private String[] listBrowser = { "FireFox", "Chrome", "Opera", "Safari", "IE"};
	
	private List<String> listDomainDefault  = new ArrayList<String>();
	private List<String> listUrlDefault = new ArrayList<String>();
	private List<String> listBrowserDefault = new ArrayList<String>();
	
	private final String fileUrlDefault   = "urldefault.txt";

	public GenDataOverStruct(long startTime, long stopTime) {
		// TODO Auto-generated constructor stub
		this.startTime = startTime;
		this.stopTime = stopTime;
		for (String s : listDomain)
			listDomainDefault.add(s);
		for (String s : listBrowser)
			listBrowserDefault.add(s);
		listUrlDefault = getDefaultDataFromFile(fileUrlDefault);
	}

	public long genTimeRequest() {
		return random.nextInt((int) (stopTime - startTime)) + startTime;
	}

	public int genUserId() {
		return random
				.nextInt((int) (MAX_INT_GUID_DEFAULT - MIN_INT_GUID_DEFAULT))
				+ MIN_INT_GUID_DEFAULT;
	}

	public String genDomain() {
		int indext = random.nextInt(listDomainDefault.size() - 1);
		return listDomainDefault.get(indext);
	}

	public String genUrl() {
		int indext = random.nextInt(listUrlDefault.size() - 1);
		return listUrlDefault.get(indext);
	}

	public int genChannelId() {
		return random.nextInt(MAX_CHANNELID_LENGTH);
	}
	
	public int genCountryId() {
		return random.nextInt(MAX_COUNTRY_LENGTH);
	}
	
	public String genBrowser() {
		int indext = random.nextInt(listBrowserDefault.size() - 1);
		return listBrowserDefault.get(indext);
	}
	
	public long genIpRequest() {
		return random.nextInt((int) (MAX_IP_DEFAULT - MIN_IP_DEFAULT))
				+ MIN_IP_DEFAULT;
	}
	
	public List<String> getDefaultDataFromFile(String file){
		List<String> result = new ArrayList<String>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(file)));
			String line = null;
			while ((line = br.readLine()) != null ){
				result.add(line);
			} 
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) throws ParseException {
		GenData genData = new GenData("2013-03-25:00-00");
		long startTime = genData.getTimeLong("2013-03-25:00-00");
		GenDataOverStruct genDataOverStruct = new GenDataOverStruct(startTime, startTime + 10 * 60 * 1000);
		System.out.println(genDataOverStruct.genBrowser());
		System.out.println(genDataOverStruct.genChannelId());
		System.out.println(genDataOverStruct.genDomain());
		System.out.println(genDataOverStruct.genIpRequest());
		System.out.println(genDataOverStruct.genTimeRequest());
		System.out.println(genDataOverStruct.genUrl());
		System.out.println(genDataOverStruct.genUserId());
		System.out.println(genDataOverStruct.genCountryId());
	}
	
}
